package com.fyw.xmcamera.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.util.List;

/**
 * Created by FYW on 2017/10/18.
 */

public class DeviceWifiManager {
    private WifiManager mWifiManager;
    private ConnectivityManager connManager;
    private static DeviceWifiManager mInstance;
    private WifiInfo mWifiInfo;

    private DeviceWifiManager(Context context) {
        if (null != context) {
            mWifiManager = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            mWifiInfo = mWifiManager.getConnectionInfo();
        } else {
            mInstance = null;
        }
    }

    public static synchronized DeviceWifiManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new DeviceWifiManager(context);
        }
        return mInstance;
    }

    public ScanResult getCurScanResult(String ssID) {
        mWifiManager.startScan();
        List<ScanResult> wifiList = mWifiManager.getScanResults();
        ScanResult scanResult = null;
        if (wifiList != null && ssID != null) {
            for (ScanResult result : wifiList) {
                if (result.SSID.contains(ssID)) {
                    scanResult = result;
                    return scanResult;
                }
            }
        }
        return scanResult;
    }
}
